package com.liming.cartype.webapp.wicket.page;


import com.liming.cartype.api.biz.ICarTypeService;
import com.liming.cartype.api.biz.IProductLineService;
import com.liming.cartype.api.persistence.impl.CarTypeBeanImpl;
import com.liming.cartype.api.persistence.impl.ProductLineBeanImpl;
import com.liming.core.web.wicket.BasePage;
import org.apache.aries.blueprint.annotation.Reference;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormValidatingBehavior;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.IChoiceRenderer;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.AbstractReadOnlyModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.time.Duration;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Cartype: sunlf
 * Date: 14-3-13
 * Time: 下午4:26
 * To change this template use File | Settings | File Templates.
 */
public class CarTypePage extends BasePage {

    @Named
    @Inject
    @Reference(id = "carTypeService", serviceInterface = ICarTypeService.class)
    private ICarTypeService carTypeService;

    @Named
    @Inject
    @Reference(id = "productLineService", serviceInterface = IProductLineService.class)
    private IProductLineService productLineService;

    public CarTypePage() {


        final Form<CarTypeBeanImpl> form = new Form<CarTypeBeanImpl>("add_carType_form", new CompoundPropertyModel<CarTypeBeanImpl>(new CarTypeBeanImpl()));
        add(form);
        AjaxFormValidatingBehavior.addToAllFormComponents(form, "keydown", Duration.ONE_SECOND);

        TextField formName = new TextField("name");
        TextField formPrice = new TextField("price");

        final  List<ProductLineBeanImpl> oneLevelKindList = productLineService.getAllProductLine();
        final DropDownChoice oneLevelKind = new DropDownChoice("productLineId",new Model(),oneLevelKindList);
        oneLevelKind.setChoiceRenderer(new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {     //设置displayName
                ProductLineBeanImpl k = (ProductLineBeanImpl) o;
                return k.getName();
            }
            public String getIdValue(Object o, int arg1) {    //设置Value
                ProductLineBeanImpl k = (ProductLineBeanImpl) o;
                return String.valueOf(k.getId());
            }
        });

        form.add(formName);
        form.add(oneLevelKind);
        form.add(formPrice);

        form.add(new AjaxButton("submit_button", form) {
            @Override
            protected void onSubmit(AjaxRequestTarget target, Form<?> submitForm) {
                String id = oneLevelKind.getModelValue();
                CarTypeBeanImpl carTypeBean = (CarTypeBeanImpl) getParent().getDefaultModelObject();
                carTypeBean.setProductLineId(id);


                CarTypeBeanImpl carTypeBean1 = carTypeService.putCarType(carTypeBean);

                ProductLineBeanImpl productLineBean = productLineService.findProductLine(id);
                if(null != productLineBean.getProducts()){
                    List<String> lineBeanList =  productLineBean.getProducts();
                    lineBeanList.add(carTypeBean1.getId());
                    productLineBean.setProducts(lineBeanList);
                    productLineService.putProductLine(productLineBean);
                }else{
                    List<String> lineBeanList = new ArrayList<String>();
                    lineBeanList.add(carTypeBean1.getId());
                    productLineBean.setProducts(lineBeanList);
                    productLineService.putProductLine(productLineBean);
                }
                setResponsePage(CarTypePage.class);
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                System.out.println("onError");
            }
        });

        add(new Link("reset_from_1") {
            @Override
            public void onClick() {
                form.setModelObject(new CarTypeBeanImpl());
            }
        });

        form.add(new Link("reset_from_2") {
            @Override
            public void onClick() {
                form.setModelObject(new CarTypeBeanImpl());
            }
        });


        List<CarTypeBeanImpl> distributorList = carTypeService.getAllCarType();
        ListDataProvider<CarTypeBeanImpl> listDataProvider = new ListDataProvider<CarTypeBeanImpl>(distributorList);
        add(new DataView<CarTypeBeanImpl>("carTypeRow", listDataProvider) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<CarTypeBeanImpl> item) {
                final CarTypeBeanImpl distributor;
                distributor = item.getModelObject();

                item.add(new Label("nameData", distributor.getName()));
                if(null != distributor.getProductLineId()){
                    ProductLineBeanImpl productLineBean = productLineService.findProductLine(distributor.getProductLineId());
                    item.add(new Label("productNameData", productLineBean.getName()));

                }else{
                    item.add(new Label("productNameData", ""));
                }
                item.add(new Label("createDate", distributor.getDate()));
                item.add(new Label("priceData", distributor.getPrice()));
                item.add(new Label("version", distributor.getNumber()));
                item.add(new Label("state", distributor.getState()));
                item.add(new Link("editParameter") {
                    @Override
                    public void onClick() {
                        PageParameters pageParameters=new PageParameters();
                        pageParameters.add("carTypeId",distributor.getId());
                        setResponsePage(CartypeParameters.class,pageParameters);
                    }
                });
                item.add(new Link("editConfiguration") {
                    @Override
                    public void onClick() {
                        setResponsePage(CartypeConfiguration.class);
                    }
                });
                item.add(new Link("editResources") {
                    @Override
                    public void onClick() {
                        setResponsePage(CartypeResources.class);
                    }
                });
                item.add(new Link("edit") {
                    @Override
                    public void onClick() {
                        CarTypeBeanImpl cartype = (CarTypeBeanImpl) getParent().getDefaultModelObject();
                        form.setModelObject(cartype);
                    }
                });
                item.add(new Link("del") {
                    @Override
                    public void onClick() {
                        CarTypeBeanImpl cartype = (CarTypeBeanImpl) getParent().getDefaultModelObject();
                        carTypeService.deleteCarType(cartype.getId());
                        setResponsePage(CarTypePage.class);
                    }
                });
                item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    public String getObject() {
                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
                    }
                }));
            }
        });


    }
}
